$(document).ready(function() {
	var time;
	var delay = 15000;
  
	var carroussel = function () { 
      $.get("/homes/show", function(data) {
        var $data = $(data);
        $(function () {
          $data.find(".state_pc").each(function() {
            if($(this).html() == 'Connecte' ) {
               $(this).empty().css('background','url("images/emblem-default_smal.png") no-repeat scroll center center transparent');
            }else{
               $(this).empty().css('background','url("images/dialog-error_smal.png") no-repeat scroll center center transparent');
            }
          });
            
          $data.find(".img_pc").click(function() {
            var id = $(this).attr("id");
            $.get("/homes/" + id + "/ajax", function(data) {
              var $data = data;
              $.blockUI({
                message: $data,
                showOverlay: true,  
                css: {
                  cursor: "normal",
                  border: "1px solid #6495ED",
                  width:'400',
                  height:'auto'
                }     
              });
              $('.blockOverlay').click($.unblockUI);
            });    
          });
          
          $data.find(".supervision").dblclick(function() {
            $.get("/homes/createsalle", function(data) {
              var $data = data;
              $.blockUI({
                message: $data,
                showOverlay: true,
                css: {
                  cursor: "normal",
                  border: "1px solid #6495ED",
                  width:'600px',
                  height:'auto'
                }     
              });
                $('.blockOverlay').click($.unblockUI);
            });    
          }); 
          
				});

       
        $('.supervision').fadeOut(100, function () {  
          $('.supervision').html($data).fadeIn(100);
           // time = setInterval(carroussel, delay);

        });
    });
	}
  
  time = setInterval(carroussel, delay);

  // Affiche les messages d erreurs dans le cas des authentifications
  if ( $("p.popup_error").html() != ""){ 
    $.blockUI({
      message: $("p.popup_error"),
      showOverlay: false,  
      css: {
        cursor: "normal",
        border: "1px solid red"
      }     
    });
    setTimeout($.unblockUI, 2000);
  }

  //verifie l'état des machines
  $(".state_pc").each(function() {
      if($(this).html() == 'Connecte' ) {
         $(this).empty().css('background','url("images/emblem-default_smal.png") no-repeat scroll center center transparent');
      }else{
         $(this).empty().css('background','url("images/dialog-error_smal.png") no-repeat scroll center center transparent');
      }
  }); 
   
  //Affiche les incidents des machines
  $(".img_pc").click(function() {
    var id = $(this).attr("id");
    $.get("/homes/" + id + "/ajax", function(data) {
      var $data = data;
      $.blockUI({
        message: $data,
        showOverlay: true,  
        css: {
          cursor: "normal",
          border: "1px solid #6495ED",
          width:'400px',
          height:'auto'
        }     
      });
        $('.blockOverlay').click($.unblockUI);
    });    
  });
  
  //Creation ajax des salles
  $(".supervision").dblclick(function() {
    $.get("/homes/createsalle", function(data) {
      var $data = data;
      $.blockUI({
        message: $data,
        showOverlay: true,
        css: {
          cursor: "normal",
          border: "1px solid #6495ED",
          width:'600px',
          height:'auto'
        }     
      });
        $('.blockOverlay').click($.unblockUI);
    });    
  });
  
   $(".help").click( function () {
    $.blockUI({
      message: $("#help"),
      showOverlay: true,  
      css: {
        cursor: "normal",
        border: "1px solid #6495ED",
        width:'400',
        height:'auto'
      }     
    });
    $('.blockOverlay').click($.unblockUI);  
  });
      //Creation ajax des salles
  // $(".box_salles_index").click(function() {
    // $.get("/homes/createmachine", function(data) {
      // var $data = data;
      // $.blockUI({
        // message: $data,
        // showOverlay: true,
        // css: {
          // top:"15px",
          // cursor: "normal",
          // border: "1px solid #6495ED",
          // width:'600px',
          // height:'auto'
        // }     
      // });

  


});
 